const App = getApp();
// 工具类
import Util from '../../../utils/util.js';
Page({
  /**
   * 页面的初始数据
   */
  onShareAppMessage() {
    return {
      title: 'picker-view',
      path: 'page/component/pages/picker-view/picker-view'
    }
  },
  data: {
    isLogin: false,
    userInfo: {}, // 用户信息
    avatarUrl:"/images/default-avatar.jpg",
    userName:"", //姓名
    userTel:"",  //电话
    userTeam:"", //服务队
    userbirthday:"2000-01-01", //生日
    userSex:"0",  //性别
    international:"", //国际编号
    userNumber:"", //身份证
    company:"",//企业名称
    introduce:"",//企业介绍
    maoHao: '：',
    userAddress:"", //联系详细地址
    userArea: "",   //联系地址省市区
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad(options) {},

  /**
   * 生命周期函数--监听页面显示
   */
  onShow() {
    let that = this;
    that.setData({
      isLogin: App.checkIsLogin()
    });
    if (that.data.isLogin) {
      //获取服务队
      // that.setData({
      //   teamId: App.checkTeam()
      // });
      // 获取当前用户信息
      that.getUserDetail();
    }
  },

  /**
   * 获取当前用户信息
   */
  getUserDetail() {
    let that = this;
    App._get('user.index/detail', {}, function(result) {
      var area = "";
      if((result.data.userInfo.province+result.data.userInfo.city+result.data.userInfo.area) != ""){
        area = result.data.userInfo.province+" "+result.data.userInfo.city+" "+result.data.userInfo.area
      }
      that.setData({
        "userInfo":result.data.userInfo,
        "avatarUrl":result.data.userInfo.avatarUrl,
        userbirthday:result.data.userInfo.birth_date??'2000-01-01',
        userSex:result.data.userInfo.genderid,
        userArea:area
      });
    });
  },
  //性别
  radioChange(e) {
    console.log('radio发生change事件，携带value值为：', e.detail.value)
    this.setData({
      userSex:e.detail.value
    })
  },
  
  //提交
  formSubmit(e) {
    var formData=e.detail.value;//获取表单数据
    let obj = {
      userName:formData.userName, //姓名
      userTel:formData.userTel,  //电话
      // userTeam:formData.userTeam, //服务队
     // international:formData.international, //国际编号
      userNumber:formData.userNumber, //身份证
      userArea:formData.userArea,//联系地址-省市区
      userAddress:formData.userAddress,//联系地址-详细地址
      company:formData.company,//企业名称
     // introduce:formData.introduce, //企业介绍
      userbirthday:this.data.userbirthday,//生日
      userSex:this.data.userSex,//性别
    };
    console.log(obj);
    let _this = this;
    App._post_form('user/edit', obj, result => {
      //成功提示
      App.showSuccess(result.msg, function() {
        wx.switchTab({
          url: '/pages/user/index',
        })
      });

    });
  },

  //textarea 光标离开触发
  bindTextAreaBlur(e){
    const that = this;
    that.setData({
      introduce:e.detail.value
    }); 
  },
// 头像
  onChooseAvatar(e) {
    console.log(111)
    const { avatarUrl } = e.detail;
    console.log(e.detail.avatarUrl);
    let av=e.detail.avatarUrl;
    let av1=wx.getFileSystemManager().readFileSync(av,'base64');
    let avatarUrl1 = "data:image/jpeg;base64,"+av1;
    console.log(avatarUrl1)
    this.setData({
      avatarUrl,
    })
    App._get('user.index/upUserInfo', {avatarUrl:avatarUrl1}, function(result) {
      console.log(result.data);
    });
    console.log(avatarUrl);
  },
  //生日
  bindDateChange: function (e) {
    console.log('picker发送选择改变，携带值为', e.detail.value)
    this.setData({
      userbirthday: e.detail.value
    })
  },
  //省市区
  bindRegionChange: function (e) {
    console.log('picker发送选择改变，携带值为', e.detail.value)
    this.setData({
      userArea:e.detail.value[0]+" "+e.detail.value[1]+" "+e.detail.value[2]
    })
  },
  // 入会\建会跳转
  apply(e){
    let type = e.currentTarget.dataset.type;
    // 入会
    if(type == 1){
      wx.navigateTo({
        url: '/pages/shanghui/index',
      })
    }
    // 创建商会
    if(type == 2){
      wx.navigateTo({
        url: '/pages/shanghui/add/index',
      })
    }
  }
})